#-------------------------------------------------------------------
# Project Invasion of the aliens 
# name game.py
# purpose can fellow an ememy with a certian distance 
#
# authors Chris L'Heureux, Tyler Plante, Jackson Morse
# main author Jackson Morse
#
# created 12/3/12
# Copyright (C) Jackson Morse 
# License New BSD
#------------------------------------------------------------------  
import pygame, math, sys, random

class Enemy():
    def __init__(self, position):
        self.baseSurfaces = []
        self.baseSurfaces += [pygame.image.load("images/Enemy/enemy1.png")]
        self.frame = 0
        self.maxFrame = len(self.baseSurfaces)-1
        self.surface = self.baseSurfaces[self.frame]
        self.rect = self.surface.get_rect() 
        self.baseRect = self.rect
        self.radius = (self.rect.width/2 +self.rect.height/2)/2
        self.angle = random.randint(0, 359) 
        self.speed = random.randint(1,3)
        self.place(position)
        self.attack = 5
        self.health = 100
        self.living = True
        self.detectionRadius = 200
        
        
        self.dir = "none"
        self.x = self.rect.center[0]
        self.y = self.rect.center[1]
        
    def  __str__(self):
        return "I'm an enemy!" + str(self.rect.center)
        
    def place(self, position):
        self.rect.center = position
        
    def move(self):
        self.newPos()
        #print "I've moved", self.speed, self.dir, self.angle
        self.rect.center = [int(self.x), int(self.y)]
        self.surface = pygame.transform.rotate(
                                self.baseSurfaces[self.frame], 
                                self.angle)
        self._clip()
                                
    def _clip(self):
        self.rect = self.surface.get_rect(center=self.rect.center)
        clipped_rect = pygame.Rect(
                (self.rect.width - self.baseRect.width) / 2,
                (self.rect.height - self.baseRect.height) / 2,
                self.baseRect.width,
                self.baseRect.height,
            )
        self.surface = self.surface.subsurface(clipped_rect)
        self.rect = self.surface.get_rect(center=self.rect.center)
     
    def collideWall(self, screenWidth, screenHeight):
        if (self.rect.left < 0 or self.rect.right > screenWidth):
                self.living = False 
        if (self.rect.top < 0 or self.rect.top > screenHeight):
                self.living = False
    
    def direction(self, dir):
        #print "moving in direction"
        return dir
        self.rect.move = player.rect.center(self.dir)
        
    def die(self):
        self.living = False    
        
    
    def distToPoint(self, pt):
        x1 = self.rect.center[0]
        x2 = pt[0]
        y1 = self.rect.center[1]
        y2 = pt[1]
        return math.sqrt(((x2-x1)**2)+((y2-y1)**2))
                            

    
    def collidePlayer(self, player):
         if (self.rect.right > player.rect.left 
             and self.rect.left < player.rect.right):
             if (self.rect.bottom > player.rect.top and 
                 self.rect.top < player.rect.bottom):
                 if (self.distToPoint(player.rect.center)
                     < self.radius + player.radius):                 
                     player.die()
                     self.die()

                   
       
    def chase(self, player):
        if self.distToPoint(player.rect.center) < self.detectionRadius:
            pX = float(player.rect.center[0])
            pY = float(player.rect.center[1])
            myX = float(self.rect.center[0])
            myY = float(self.rect.center[1])
            
            if myX < pX and myY == pY:
                angle = 0
                #print "+X"
            elif myX < pX and myY > pY: #Quad 1
                diffX = (pX - myX)
                diffY = (pY - myY)*-1
                self.angle = math.degrees(math.atan(diffY/diffX))
                #print "Qaud 1"
                #print "(", diffX, ",", diffY, ")", self.angle
            elif myX == pX and myY > pY:
                angle = 90
                #print "+Y"
            elif myX > pX and myY > pY: #Quad 2
                diffX = (pX - myX)
                diffY = (pY - myY)*-1
                self.angle = 180 - math.degrees(math.atan(diffY/diffX * -1))
                #print "Qaud 2"
                #print "(", diffX, ",", diffY, ")", self.angle
            elif myX > pX and myY == pY:
                angle = 180
                #print "-X"
            elif myX > pX and myY < pY:
                diffX = (pX - myX)
                diffY = (pY - myY)*-1
                self.angle = 180 + math.degrees(math.atan(diffY * -1/diffX * -1))
                #print "Qaud 3"
                #print "(", diffX, ",", diffY, ")", self.angle
            elif myX == pX and myY < pY:
                self.angle = 270
                #print "-Y"
            elif myX < pX and myY < pY:
                diffX = (pX - myX)
                diffY = (pY - myY)*-1
                self.angle = 360 - math.degrees(math.atan(diffY * -1/diffX))
                #print "Qaud 4"
                #print "(", diffX, ",", diffY, ")", self.angle
               
            
            

  
  
    def newPos(self):
        #print ">>>>enemy1>>>>>>>>>>>",self.angle,"<<<<<<<<<<<<<<<<<<<"
        self.angle = self.angle % 360
        if self.angle < 0:
            self.angle = 360 - math.abs(self.angle)
            
        if self.angle == 0:
            self.x += self.speed
            self.y += 0
        elif 0 < self.angle < 90: 
            self.x += self.speed * math.cos(math.radians(self.angle))
            self.y += -self.speed * math.sin(math.radians(self.angle))
        elif self.angle == 90:
            self.x += 0
            self.y += -self.speed
        elif 90 < self.angle < 180:
            self.x += -self.speed * math.sin(math.radians(self.angle-90))
            self.y += -self.speed * math.cos(math.radians(self.angle-90))
        elif self.angle == 180:
            self.x += -self.speed
            self.y += 0
        elif 180 < self.angle < 270:
            self.x += -self.speed * math.cos(math.radians(self.angle-180))
            self.y += self.speed * math.sin(math.radians(self.angle-180))
        elif self.angle == 270:
            self.x += 0
            self.y += self.speed 
        elif 270 < self.angle < 360:
            self.x += self.speed * math.sin(math.radians(self.angle-270))
            self.y += self.speed * math.cos(math.radians(self.angle-270))
    
    def collideEnemy(self, other):
        if (self.rect.right > other.rect.left 
            and self.rect.left < other.rect.right):
            if (self.rect.bottom > other.rect.top and 
                self.rect.top < other.rect.bottom):
                if (self.distToPoint(other.rect.center)
                    < self.radius + other.radius):
                    self.die()
                    other.die()
    #TSET
        
       

    
